import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/show")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 通过 api 把得到的结果 构造成字符串，统一返回到客户端
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(req.getProtocol());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getMethod());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getRequestURI());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getContextPath());
        stringBuffer.append("<br>");
        stringBuffer.append(req.getQueryString());
        stringBuffer.append("<br>");

        // 获取所有的 header
        Enumeration<String> header = req.getHeaderNames();
        while (header.hasMoreElements()) {
            String key = header.nextElement();
            String value = req.getHeader(key);
            stringBuffer.append(key + ":" + value + "<br>");
        }

        // 告诉浏览器数据类型是啥
        resp.setContentType("text/html; charset=utf8");

        // 返回到客户端
        resp.getWriter().write(stringBuffer.toString());
    }
}
